package com.muqi.app.qlearn.student.ui;

import java.util.HashMap;

import com.muqi.app.network.http.MyAsyncHttp;
import com.muqi.app.network.http.MyAsyncHttp.AsyncCallBackListener;
import com.muqi.app.network.http.ParamsUtils;
import com.muqi.app.network.http.MyAsyncHttp.RESULTCODE;
import com.muqi.app.project.contants.NetWorkApi;
import com.muqi.app.qlearn.student.BaseFragmentActivity;
import com.muqi.app.qlearn.student.R;
import com.wenhui.pedant.SweetAlert.SweetAlertDialog;
import com.wenhui.pedant.SweetAlert.SweetAlertDialog.OnSweetClickListener;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class FeedbackActivity extends BaseFragmentActivity implements
		OnClickListener {
	private TextView tv_remian;
	private Button bt_commit;
	private EditText et_content;
	private EditText et_contact;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_feedback);

		tv_remian = (TextView) findViewById(R.id.tv_remian);
		bt_commit = (Button) findViewById(R.id.bt_commit);
		et_content = (EditText) findViewById(R.id.content);
		et_contact = (EditText) findViewById(R.id.contact);
		tv_remian.setOnClickListener(this);
		bt_commit.setOnClickListener(this);
		et_content.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(final CharSequence arg0, int arg1,
					int arg2, int arg3) {
				if (arg0.length() <= 500) {
					tv_remian.setText((500 - arg0.length()) + "");

				} else {
					new SweetAlertDialog(FeedbackActivity.this,
							SweetAlertDialog.WARNING_TYPE)
							.setTitleText("已超过最大字数")
							.setConfirmText("确定")
							.showCancelButton(true)

							.setConfirmClickListener(
									new OnSweetClickListener() {
										@Override
										public void onClick(
												SweetAlertDialog mDialog) {
											mDialog.dismiss();
											et_content.setText(arg0
													.subSequence(0, 499));
										}
									}).show();
				}

			}

			@Override
			public void afterTextChanged(Editable arg0) {
				// TODO Auto-generated method stub

			}
		});

	}

	public void onBack(View v) {
		this.finish();
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.bt_commit:
			if (checkInput()) {
				submitIdea();
			}
			break;
		default:
			break;
		}

	}

	private void submitIdea() {
		HashMap<String, Object> sendMap = new HashMap<String, Object>();
		sendMap.put("token", mSpUtil.getToken());
		sendMap.put("content", et_content.getText().toString());
		sendMap.put("contact", et_contact.getText().toString());
		String url = ParamsUtils.buildParams(NetWorkApi.FADEBACK_API, sendMap);
		MyAsyncHttp.get(mContext, url, new AsyncCallBackListener() {

			@Override
			public void onAsyncCallBack(RESULTCODE resuleCode, String request,
					String result) {
				if (resuleCode == RESULTCODE.SUCCESS) {
					new SweetAlertDialog(FeedbackActivity.this,
							SweetAlertDialog.NORMAL_TYPE)
							.setTitleText("已收到您的宝贵意见，我们会尽快处理")
							.setConfirmText("确定")
							.showCancelButton(true)

							.setConfirmClickListener(
									new OnSweetClickListener() {
										@Override
										public void onClick(
												SweetAlertDialog mDialog) {
											mDialog.dismiss();
											finish();

										}
									}).show();
				} else {
					showToast("提交失败");
				}

			}
		});
	}

	private boolean checkInput() {
		if (TextUtils.isEmpty(et_content.getText())) {
			showToast("内容不能为空");
			return false;
		} else if (TextUtils.isEmpty(et_contact.getText())) {
			showToast("联系方式不能为空");
			return false;
		}
		return true;
	}

	@Override
	protected void onInit() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onData() {
		// TODO Auto-generated method stub

	}

}
